package br.com.lh.urabuilder.helper
{
	import br.com.lh.urabuilder.helper.dragdrop.IDragAndDropDelegator;
	import br.com.lh.urabuilder.util.BorderDrawer;
	import br.com.lh.urabuilder.view.arrow.ArrowContainer;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.DragSource;

	public class ArrowMover
	{

		private var mouseEventHelper:MouseEventHelper;
		public var movingArrow:ArrowContainer;
		private var dragDropDelegator:IDragAndDropDelegator;

		public function ArrowMover(mouseEventHelper:MouseEventHelper,dragDropDelegator:IDragAndDropDelegator)
		{
			this.mouseEventHelper=mouseEventHelper;
			this.dragDropDelegator = dragDropDelegator;
		}

		public function rotateStartPoint(newPoint:Point):Boolean
		{
			if (!mouseEventHelper.isMouseRotatingStartArrow)
			{
				return false;
			}
			var endPoint:Point=movingArrow.endPoint;
			movingArrow.removeWrapper();
			movingArrow.doDraw(newPoint, endPoint);
			BorderDrawer.drawBorder(movingArrow, movingArrow.selected);
			return true;
		}

		public function rotateEndPoint(newPoint:Point):Boolean
		{
			if (!mouseEventHelper.isMouseRotatingEndArrow)
			{
				return false;
			}
			var startPoint:Point=movingArrow.startPoint;
			movingArrow.removeWrapper();
			movingArrow.doDraw(startPoint, newPoint);
			BorderDrawer.drawBorder(movingArrow, movingArrow.selected);
			return true;
		}

		public function moveArrow():Boolean
		{
			return false;
		}


		private function initDrag(event:MouseEvent):void
		{
			if (!mouseEventHelper.isMouseMovingArrow)
			{
				return ;
			}
			var ds:DragSource=new DragSource();
			ds.addData(event.localX, 'localx');
			ds.addData(event.localY, 'localy');
			ds.addData("arrow","type");
			dragDropDelegator.startDragging(movingArrow, ds, event, movingArrow);
		}


		public function mouseDown(event:MouseEvent):void
		{
			if (mouseEventHelper.isArrowDraggingAllowed())
			{
				movingArrow=ArrowContainer(event.currentTarget);
				if (event.localX > movingArrow.width * ArrowContainer.END_OF_ARROW_PERCENT)
				{
					mouseEventHelper.isMouseRotatingEndArrow=true;
				}
				else if (event.localX < movingArrow.width * ArrowContainer.START_OF_ARROW_PERCENT)
				{
					mouseEventHelper.isMouseRotatingStartArrow=true;
				}
				else
				{
					mouseEventHelper.isMouseMovingArrow=true;
//					initDrag(event);
				}

			}
		}
	}
}


